package com.innovation.chengzhangriji.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;

import com.innovation.chengzhangriji.Baby;
import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.Discover;
import com.innovation.chengzhangriji.DiscoverPhoto;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.database.DatabaseManager;
import com.innovation.chengzhangriji.ui.adapter.HistoryPhotoAdapter;
import com.innovation.chengzhangriji.ui.adapter.HistoryPhotoContentHolder;
import com.innovation.chengzhangriji.ui.model.UIHistoryPhoto;
import com.innovation.chengzhangriji.ui.model.UIHistoryPhotoContent;
import com.innovation.chengzhangriji.ui.model.UIHistoryPhotoDate;
import com.innovation.chengzhangriji.util.BabyUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

public class HistoryPhotoActivity extends BaseHistoryPhotoAndVideoActivity implements HistoryPhotoAdapter.HistoryPhotoAgent, HistoryPhotoContentHolder.PhotoClickListener {

    private List<String> mUrls = new ArrayList<>();
    private List<Long> mTss = new ArrayList<>();
    private List<String> mThemes = new ArrayList<>();
    private List<String> mTexts = new ArrayList<>();
    private List<Integer> mStates = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        textEmptyView.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.empty_photo), null, null);
        textEmptyView.setText("还没有照片哦~ 快快去拍照吧!\n咔嚓咔嚓~ SO EASY!");
    }

    @Override
    public void onClickLeft() {
        finish();
    }

    @Override
    public void onClickRight() {

    }

    @Override
    public void onPhotoClick(HistoryPhotoContentHolder holder, int index) {
        if(holder != null && holder.getLayoutPosition() != RecyclerView.NO_POSITION) {
            int position = holder.getLayoutPosition();
            Intent intent = new Intent(this, HistoryGalleryActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("url", ((UIHistoryPhotoContent) photos.get(position)).getThumbnailUrls().get(index));
            String[] urls = new String[mUrls.size()];
            mUrls.toArray(urls);
            bundle.putStringArray("urls", urls);
            long[] tss = new long[mTss.size()];
            for(int i = 0; i < mTss.size(); i++) {
                tss[i] = mTss.get(i);
            }
            bundle.putLongArray("createTSs", tss);
            String[] themes = new String[mThemes.size()];
            mThemes.toArray(themes);
            bundle.putStringArray("themes", themes);
            String[] texts = new String[mTexts.size()];
            mTexts.toArray(texts);
            bundle.putStringArray("texts", texts);
            int[] states = new int[mStates.size()];
            for(int i = 0; i < mStates.size(); i++) {
                states[i] = mStates.get(i);
            }
            bundle.putIntArray("states", states);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    }

    @Override
    protected String getCuteTitle() {
        return "照片";
    }

    // 转换数据格式
    @Override
    protected void setupData() {
        photos.clear();
        mUrls.clear();
        mTexts.clear();
        mThemes.clear();
        mTss.clear();
        mStates.clear();

        // 按时间倒叙排序
        Collections.sort(models, new Comparator<Discover>() {
            @Override
            public int compare(Discover lhs, Discover rhs) {
                return rhs.getCreateTS().compareTo(lhs.getCreateTS());
            }
        });
        Date lastCreateMonth = new Date();
        String lastAge = null;
        List<String> historyPhotos = null;
        for(int i = 0; i < models.size(); i++) {
            if(models.get(i).getGrowthStageID() == 1) {
                Date date = new Date(models.get(i).getCreateTS());
                if(i == 0 || date.getYear() != lastCreateMonth.getYear() || date.getMonth() != lastCreateMonth.getMonth()) {
                    lastCreateMonth = date;
                    // 添加一个时间轴
                    UIHistoryPhotoDate dateItem = new UIHistoryPhotoDate();
                    dateItem.setDate("备孕中 " + (date.getYear() + 1900) +"年" + (date.getMonth() + 1) + "月");
                    photos.add(dateItem);
                    UIHistoryPhotoContent content = new UIHistoryPhotoContent();
                    historyPhotos = new ArrayList<>();
                    content.setThumbnailUrls(historyPhotos);
                    content.setTheme(models.get(i).getTheme());
                    photos.add(content);
                }
            } else { // 孕期和成长期
                String currentAge = BabyUtil.getBabyDay(mBaby, models.get(i).getGrowthStageID(), models.get(i).getCreateTS());
                if(models.get(i).getGrowthStageID() == 2) {
                    int index = currentAge.indexOf("周");
                    if(index == -1) {
                        currentAge = "孕0周";
                    } else {
                        currentAge = currentAge.substring(0, index + 1);
                    }
                } else {
                    int index = currentAge.indexOf("月");
                    if(index == -1) {
                        String age = "";
                        index = currentAge.indexOf("岁");
                        if(index != -1) {
                            age = currentAge.substring(0, index + 1);
                        }
                        currentAge = age + "0个月";
                    } else {
                        currentAge = currentAge.substring(0, index + 1);
                    }
                }
                if(lastAge == null || !lastAge.equals(currentAge)) {
                    lastAge = currentAge;
                    // 添加一个时间轴
                    UIHistoryPhotoDate dateItem = new UIHistoryPhotoDate();
                    dateItem.setDate(lastAge);
                    photos.add(dateItem);
                    UIHistoryPhotoContent content = new UIHistoryPhotoContent();
                    historyPhotos = new ArrayList<>();
                    content.setThumbnailUrls(historyPhotos);
                    content.setTheme(models.get(i).getTheme());
                    photos.add(content);
                }
            }
            // 添加日记的照片
            List<DiscoverPhoto> photos = DatabaseManager.getInstance(HistoryPhotoActivity.this).getDiscoverPhotos(models.get(i).getDiaryID());
            for(int j = 0; j < photos.size(); j++) {
                mUrls.add(photos.get(j).getUrl());
                mTss.add(models.get(i).getCreateTS());
                mThemes.add(models.get(i).getTheme());
                mTexts.add(models.get(i).getText());
                mStates.add(models.get(i).getGrowthStageID());
                historyPhotos.add(photos.get(j).getUrl());
            }
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    protected int getType() {
        return 2;
    }

    @Override
    protected RecyclerView.Adapter<RecyclerView.ViewHolder> getAdapter() {
        return new HistoryPhotoAdapter(this);
    }

    @Override
    public HistoryPhotoContentHolder.PhotoClickListener getPhotoClickListener() {
        return this;
    }

    @Override
    public List<UIHistoryPhoto> getItemList() {
        return photos;
    }
}
